using UnityEngine;
using UnityEditor;

namespace TCC.Framework
{
    /// <summary>
    /// 编辑器文件路径工具类
    /// </summary>
    public static class AssetPathTool
    {
        /// <summary>
        /// 获取资源绝对路径
        /// </summary>
        /// <param name="obj">资源</param>
        /// <returns></returns>
        public static string GetAssetAbsolutePath(Object obj) => RelativePathToAbsPath(GetAssetRelativePath(obj));

        /// <summary>
        /// 获取资源绝对路径
        /// </summary>
        /// <param name="instanceID">资源instanceID</param>
        /// <returns></returns>
        public static string GetAssetAbsolutePath(int instanceID) => RelativePathToAbsPath(GetAssetRelativePath(instanceID));

        /// <summary>
        /// 获取资源相对路径
        /// </summary>
        /// <param name="obj">资源</param>
        /// <returns></returns>
        public static string GetAssetRelativePath(Object obj) => AssetDatabase.GetAssetPath(obj);

        /// <summary>
        /// 获取资源相对路径
        /// </summary>
        /// <param name="instanceID">资源ID</param>
        /// <returns></returns>
        public static string GetAssetRelativePath(int instanceID) => AssetDatabase.GetAssetPath(instanceID);

        /// <summary>
        /// 获取资源文件夹绝对路径
        /// </summary>
        /// <param name="obj">资源</param>
        /// <returns></returns>
        public static string GetAssetFileAbsolutePath(Object obj)
        {
            string path = GetAssetAbsolutePath(obj);
            return path.Remove(path.LastIndexOf('/'));
        }

        /// <summary>
        /// 获取资源文件夹绝对路径
        /// </summary>
        /// <param name="instanceID">资源ID</param>
        /// <returns></returns>
        public static string GetAssetFileAbsolutePath(int instanceID)
        {
            string path = GetAssetAbsolutePath(instanceID);
            return path.Remove(path.LastIndexOf('/'));
        }

        /// <summary>
        /// 获取资源文件夹相对路径
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetAssetFileRelativePath(Object obj) => AbsPathToRelativePath(GetAssetFileAbsolutePath(obj));

        /// <summary>
        /// 获取资源文件夹相对路径
        /// </summary>
        /// <param name="instanceID">资源ID</param>
        /// <returns></returns>
        public static string GetAssetFileRelativePath(int instanceID) => AbsPathToRelativePath(GetAssetFileAbsolutePath(instanceID));

        /// <summary>
        /// 获取打开文件的绝对路径
        /// </summary>
        /// <param name="title">打开文件面板标题</param>
        /// <param name="directory">打开文件时的默认打开的文件夹路径</param>
        /// <param name="extension">文件后缀</param>
        /// <returns>弹出选择文件打开面板，选定文件后返回文件绝对路径</returns>
        public static string GetOpenFileAbsolutePath(string title, string directory, string extension)
        {
            return EditorUtility.OpenFilePanel(title, directory, extension);
        }
        
        /// <summary>
        /// 获取打开文件的相对路径
        /// </summary>
        /// <param name="title">打开文件面板标题</param>
        /// <param name="directory">打开文件时的默认打开的文件夹路径</param>
        /// <param name="extension">文件后缀</param>
        /// <returns>弹出选择文件打开面板，选定文件后返回文件相对路径</returns>
        public static string GetOpenFileRelativePath(string title, string directory, string extension)
        {
            return AbsPathToRelativePath(GetOpenFileAbsolutePath(title, directory, extension));
        }

        /// <summary>
        /// 绝对路径转相对路径
        /// </summary>
        /// <param name="path">绝对路径</param>
        /// <returns></returns>
        public static string AbsPathToRelativePath(string path)
        {
            if (path.StartsWith(FilePathTool.ProjectAbsolutePath))
                return path.Replace(FilePathTool.ProjectAbsolutePath, "Assets/");
            else if (path.StartsWith("Assets"))
                return path;
            else
            {
                Debug.Log($"路径【{path}】不是Unity路径！");
                return "";
            }
        }

        /// <summary>
        /// 相对路径转绝对路径
        /// </summary>
        /// <param name="path">相对路径</param>
        /// <returns></returns>
        public static string RelativePathToAbsPath(string path)
        {
            if (path.StartsWith("Assets"))
                return FilePathTool.ProjectAbsolutePath+path.Substring(path.IndexOf("Assets")+6);
            else if (path.StartsWith(FilePathTool.ProjectAbsolutePath))
                return path;
            else
            {
                Debug.Log($"路径【{path}】不是Unity路径！");
                return "";
            }
        }
    }
}
